function $l(o,e,f,b){ return o.addEventListener(e,f,b); }
function $e(name, content){
  var e = document.createElement(name);
  if (content) { e.innerHTML = content; }
  return e;
}

function $getTags(el, tag)
{
	//alert(el.innerHTML);
	var tokens = el.innerHTML.split(tag);
	var filesArray = [(tokens.length-1)*2]; //excluding the root folder
	var tempCounter=0;
	for(var i=1; i<tokens.length; i++) //skipping the root folder
	{ //for each child dir/file
		var fileTokens = tokens[i].split(" ");
		filesArray[tempCounter++] = fileTokens[1];
		//document.getElementById("log").innerHTML += "<br>" + filesArray[tempCounter-1];
		filesArray[tempCounter++] = fileTokens[4];
		//document.getElementById("log").innerHTML += "<br>" + filesArray[tempCounter-1];
	}
	return filesArray;
}

FileBrowser = function(el,rootdir) {
  this.el = el;
  this.root = rootdir;
  this.goTo(this.root);
};

FileBrowser.prototype = {
  read : function(path) {
	var x = null;
	if (window.XMLHttpRequest) {              
      x=new XMLHttpRequest();              
    } else {                                  
      x=new ActiveXObject("Microsoft.XMLHTTP");
    } 
    //var x = new XMLHttpRequest()
    x.open("GET", path, false);
    x.send(null);
    return x.responseText;
  },
  goTo : function(dir) {
    var d = $e('div', this.read(dir));
    var anchors = $getTags(d, '201:');
    //files = [];
    //var h = $e('h3');
    //h.appendChild(document.createTextNode(dir));
    //this.el.appendChild(h);
    var mdir = dir.replace(/\/$/,'');
    for(var i = 0; i < anchors.length; i=i+2) {
	  var fName = anchors[i];
	  var fType = anchors[ i+1 ];
	  var a = $e('a',Util.stringReplaceAll(fName,"%20"," "));
	  a.href = ""+dir+"/"+fName;
      if (fType == "DIRECTORY" || Util.isFlashObj(fName) || Util.isImage(fName) || Util.isJavascript(fName)) {
        //if (a.href.length >= this.root.length) {
          //a.target = '_new';
		  var dragCtrl;
          if (fType == 'DIRECTORY'){
			a = this.makeLive(a, mdir);
			this.el.appendChild(a);
			//this.el.appendChild($e('br'));
		  }else if(Util.isImage(fName)){
		  	var img = $e('img');
			img.src = a.href;
			img.className = "browserImg";
			img.style.width = "100px";
			this.el.appendChild(img);
			
			dragCtrl = new Draggable(img, {revert:true});
		  }
		  else if(Util.isJavascript(fName)){
		  	var jsImg = $e('div');
			jsImg.innerHTML = Util.getScriptIconHTML() + " &nbsp;"+fName+"<p style=\"display:none\">"+a.href+"</p>";
			jsImg.className = "browserImg";
			this.el.appendChild(jsImg);
			
			dragCtrl = new Draggable(jsImg, {revert:true});
		  }
		  else if(Util.isFlashObj(fName)){
		  	var swfImg = $e('div');
			swfImg.innerHTML = Util.getScriptIconHTML() + " &nbsp;"+fName+"<p style=\"display:none\">"+a.href+"</p>";
			swfImg.className = "browserImg";
			this.el.appendChild(swfImg);
			
			dragCtrl = new Draggable(swfImg, {revert:true});
		  }
        //}
      }
    }
  },
  makeLive : function(a,mdir) {
    var d = $e("div");	
	d.className = "browserDirectory";
    d.browser = null;
    d.appendChild(a);
    $l(a, 'click', function(e){ 
      if(this.parentNode.browser) {
        this.parentNode.removeChild(this.parentNode.browser);
        this.parentNode.browser = null;
      } else {
        var b = $e('div');
		b.className = "browserDirectory";
        var fb = new FileBrowser(b, a.href);
        this.parentNode.browser = b;
        this.parentNode.appendChild(b);
      }
      if (e.preventDefault){ e.preventDefault(); }
      return false ;
    }, false);
    a.onclick = function(){ return false; };
    return d;
  }
};

var d;
function fileBrowser_init(){
	clearFileBrowser();
	var fileLoc = "";
	if(Util.getOS() == "windows"){
		fileLoc = "file:///C:";
	}else{
		document.getElementById("fileBrowserWindow.driveTxt").style.display="none";
		fileLoc = "file:///";
	}
	//fileBrowser_start();
}

function fileBrowser_start(){
	d = new FileBrowser(document.getElementById('fileBrowserWindow'), fileLoc);
}

function fileBrowser_update(){
	clearFileBrowser();
	var drive = document.getElementById("fileBrowserWindow.driveTxt").value;
	d = new FileBrowser(document.getElementById('fileBrowserWindow'), "file:///"+drive);
}
function clearFileBrowser(){
	var childNodesList = document.getElementById("fileBrowserWindow").childNodes;
	var len = childNodesList.length;
	var index = 0;
	while(index < len)
	{
		var tempDiv = childNodesList[index];
		if(isOfType(tempDiv,'div'))
		{
			document.getElementById("fileBrowserWindow").removeChild(tempDiv);
		}else{
			index++;
		}
	}
}
//$l(window,'load',fileBrowser_init,false);


function addFile(element, wasDropped){
	var location = "";
	if(element instanceof HTMLImageElement){
		location = element.src;
	}else if(element instanceof HTMLDivElement){
		var childNodes = element.childNodes;
		var count = 0;
		while(count < childNodes.length)
		{
			var divElement = childNodes.item(count);
			if(divElement instanceof HTMLParagraphElement)
			{
				location = divElement.innerHTML;
				break;
			}
			count++;
		}
	}

	if(Util.isImage(location)){
		addImage(location,wasDropped);
	}else if(Util.isJavascript(location)){
		addJavascript(location);
		alert("added javascript");
	}else if(Util.isFlashObj(location)){
		addFlashObj(location,wasDropped);
	}
	
}